import { _decorator, Component, director, Node, ProgressBar, resources } from 'cc';
import { SCENE_ENUM } from '../../Enums';
const { ccclass, property } = _decorator;

@ccclass('LoadingManager')
export class LoadingManager extends Component {
    @property(ProgressBar)
    bar: ProgressBar = null;

    protected onLoad(): void {
        this.preLoad();
        director.preloadScene(SCENE_ENUM.Loading);
    }

    preLoad() {
        resources.preloadDir('texture', (finished: number, total: number, _) => {
            this.bar.progress = finished / total;
        }, async (err) => {
            if (err) {
                await new Promise(resolve => {
                    setTimeout(resolve, 2000);
                });
                this.preLoad();
                return;
            }

            director.loadScene(SCENE_ENUM.Start);
        });
    }
}

